package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Order;
import com.itheima.service.OrderSettingListService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Map;

//有事务（@Transactional）就写 interfaceClass = OrderSettingListService.class
@Service
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private OrderSettingListDao orderSettingListDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public PageResult pageQuery (Map map) {
        Integer currentPage = (Integer) map.get("currentPage");
        Integer pageSize = (Integer) map.get("pageSize");
        String queryString = (String) map.get("queryString");
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        String orderType = (String) map.get("orderType");
        String orderStatus = (String) map.get("orderStatus");
        PageHelper.startPage(currentPage, pageSize);
        Page<Map> page = orderSettingListDao.selectByCondition(queryString, startDate, endDate, orderType, orderStatus);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (Map mapData : page) {
            String orderDate = format.format(mapData.get("orderDate"));
            mapData.put("orderDate", orderDate);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }


    public void setStatus(Integer id) {
        Order order = orderDao.findStatusById(id);
        String orderStatus = order.getOrderStatus();
        if(orderStatus.equals(order.ORDERSTATUS_YES)){
            orderSettingListDao.setStatus(order.ORDERSTATUS_NO,id);
        }else{
            orderSettingListDao.setStatus(order.ORDERSTATUS_YES,id);
        }
    }
}

